//
//  LXDNewSongViewController.m
//  BLDMusics
//
//  Created by lxd on 16/4/5.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "LXDNewSongViewController.h"
#import "DSYCarouselModel.h"
#import "DSYITCollectionViewCell.h"
@interface LXDNewSongViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *modelArr;
@property (nonatomic, assign) NSInteger page;

@end

@implementation LXDNewSongViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createCollection];
    [self dataHandle];
    [self addFooter];
}


- (void)createCollection {
    _page = 1;
    self.modelArr = [NSMutableArray array];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing  = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.itemSize = CGSizeMake((SCREEN_WIDTH - 30) / 2, (SCREEN_WIDTH - 30) / 2);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH - 60 - 49) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerClass:[DSYITCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _modelArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LXDNewSongModel *model = _modelArr[indexPath.row];
    DSYITCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    DSYCarouselModel *model1 = [[DSYCarouselModel alloc] init];
    model1.name = model.name;
    model1.picUrl = model.picUrl;
    cell.sourceModel  = model1;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)dataHandle {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [APPTools GETWithURL:[NSString stringWithFormat:@"http://api.dongting.com/misc/album/new?page=%ld&size=30", _page] par:nil success:^(id responseObject) {
        NSArray *tempArr = [LXDNewSongModel modelHandleWithArray:responseObject[@"data"]];
     
        [self.modelArr addObjectsFromArray:tempArr];
        [hud removeFromSuperview];
        // 结束上拉刷新
        [_collectionView.mj_footer endRefreshing];
        
        [_collectionView reloadData];
        
    } filed:^(NSError *error) {
        
    }];
}

- (void)addFooter {
    // 上拉刷新-默认
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self dataHandle];
    }];
    _collectionView.mj_footer.automaticallyHidden = YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
